package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CustomException;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.service.CategoryService;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分类管理
 */
//@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {


    private static Logger log = LogManager.getLogger(CategoryController.class);

    @Autowired
    private CategoryService categoryService;


    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        //日志
        log.info("category: {}", category);
        //非空判断
        if (null == category) return R.error("参数不能为空！");
        //调用方法
        categoryService.save(category);
        return R.success("新增分类成功！");
    }


    /**
     * 分页查询所有
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        log.info("当前页码：{}，每页展示数：{}", page, pageSize);

        //构造分页构造器
        Page<Category> pageInfo = new Page<Category>(page, pageSize);

        //构造条件构造器  升序
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();
        queryWrapper.orderByAsc(Category::getSort);  //根据升序排序

        //调用方法
        categoryService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);

    }


    /**
     * 根据id删除
     *
     * @param ?id=xxxx
     * @return
     */
    @DeleteMapping
    public R<String> deleteById(Long id) {
        //
        log.info("id值： {}", id);
        if (null == id) return R.error("id为空！");
        //调用方法
        //categoryService.removeById(id);

        //Service层自定义的方法 方式一
        /*categoryService.remove(id);
        return R.success("分类信息删除成功");*/
        return categoryService.remove(id);
    }


    /**
     * 根据id查询分类数据回显
     */
    @GetMapping("/{id}")
    public R<Category> getInfoById(@PathVariable Long id) {
        log.info("需要查询回显的分类id:{}", id);
        //判断
        if (null == id) return R.error("id为空，查询失败");
        //查询
        Category category = categoryService.getById(id);
        //返回
        if (null != category) {
            return R.success(category);
        }
        return R.error("查询数据为空");
    }


    /**
     * 修改
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        //
        log.info("修改信息： {}", category);
        if(null==category)return R.error("分类数据为空，无法修改");

        //调用方法
        categoryService.updateById(category);

        return R.success("修改分类信息成功");
    }


    /**
     * 根据菜品分类type=1查询所有菜品分类   回显数据到添加菜品页面右上角
     * 根据套餐分类type=2查询所有套餐分类   回显数据到添加套餐页面右上角
     * 将传过来的 url?type = 1 or 2 封装到categry实体或者 Integer type 都可以接收
     *
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        log.debug(category.toString());

        //判断
        if(null==category && null==category.getType()){
            throw new CustomException("分类类型参数为空!");
        }
        //构造条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>();
        //添加具体条件
        queryWrapper.eq(null!=category.getType() , Category::getType, category.getType());
        //添加  排序条件         升序                           修改时间降序
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);

        //调用方法
        List<Category> list = categoryService.list(queryWrapper);

        return R.success(list);
    }

}
